import { createRouter, createWebHashHistory } from 'vue-router'
import Index from '../views/index.vue'
import BusinessList from '../views/BusinessList.vue'
import BusinessInfo from '../views/BusinessInfo.vue'
import Login from '../views/Login.vue'
import Orders from '../views/Orders.vue'
import UserAddress from '../views/UserAddress.vue'
import Payment from '../views/Payment.vue'
import OrderList from '../views/OrderList.vue'
import AddUserAddress from '../views/AddUserAddress.vue'
import EditUserAddress from '../views/EditUserAddress.vue'
import Register from '../views/Register.vue'
import UserInfo from '../views/UserInfo.vue'
import alterUserInfo from '../views/AlterUserInfo.vue'
import superMember from '../views/SuperMember.vue'
import ApiTest from '../views/ApiTest.vue'
import BusinessLogin from '../views/BusinessLogin.vue'
import BusinessRegister from '../views/BusinessRegister.vue'
import BusinessManage from '../views/BusinessManage.vue'
import FoodManage from '../views/FoodManage.vue'
import BusinessOrders from '../views/BusinessOrders.vue'
import BusinessOrderDetail from '../views/BusinessOrderDetail.vue'
import BusinessStoreSelector from '../views/BusinessStoreSelector.vue'
import Settings from '../views/Settings.vue'
import Discovery from '../views/Discovery.vue'
import OrderDetails from '../views/OrderDetails.vue'
import BusinessRefunds from '../views/BusinessRefunds.vue'
import BusinessWallet from '../views/BusinessWallet.vue'
import Wallet from '../views/Wallet.vue'
import WalletRecharge from '../views/WalletRecharge.vue'
import WalletWithdraw from '../views/WalletWithdraw.vue'
import WalletTransactions from '../views/WalletTransactions.vue'
import WalletTransfer from '../views/WalletTransfer.vue'
import WalletRules from '../views/WalletRules.vue'
import WalletOverdraft from '../views/WalletOverdraft.vue'

// 定义需要登录才能访问的路由
const authRequiredRoutes = [
  'Orders', 'UserAddress', 'Payment', 'OrderList',
  'AddUserAddress', 'EditUserAddress', 'UserInfo',
  'AlterUserInfo', 'SuperMember', 'Settings', 'OrderDetails', 'Wallet', 'WalletRecharge', 'WalletWithdraw', 'WalletTransactions', 'WalletRules', 'WalletOverdraft'
];

// 定义需要商家登录才能访问的路由
const businessAuthRequiredRoutes = [
  'BusinessManage', 'FoodManage', 'BusinessOrders', 'BusinessOrderDetail',
  'BusinessRefunds', 'BusinessWallet'
];

const routes = [{
  path: '/',
  name: 'Home',
  component: Index
}, {
  path: '/index',
  name: 'Index',
  component: Index
}, {
  path: '/businessList',
  name: 'BusinessList',
  component: BusinessList
}, {
  path: '/businessInfo',
  name: 'BusinessInfo',
  component: BusinessInfo
}, {
  path: '/login',
  name: 'Login',
  component: Login
}, {
  path: '/orders',
  name: 'Orders',
  component: Orders,
  meta: { requiresAuth: true }
}, {
  path: '/userAddress',
  name: 'UserAddress',
  component: UserAddress,
  meta: { requiresAuth: true }
}, {
  path: '/payment',
  name: 'Payment',
  component: Payment,
  meta: { requiresAuth: true }
}, {
  path: '/orderList',
  name: 'OrderList',
  component: OrderList,
  meta: { requiresAuth: true }
}, {
  path: '/order-details/:orderId',
  name: 'OrderDetails',
  component: OrderDetails,
  meta: { requiresAuth: true }
}, {
  path: '/addUserAddress',
  name: 'AddUserAddress',
  component: AddUserAddress,
  meta: { requiresAuth: true }
}, {
  path: '/editUserAddress',
  name: 'EditUserAddress',
  component: EditUserAddress,
  meta: { requiresAuth: true }
}, {
  path: '/register',
  name: 'Register',
  component: Register
}, {
  path: '/userInfo',
  name: 'UserInfo',
  component: UserInfo,
  meta: { requiresAuth: true }
}, {
  path: '/alterUserInfo',
  name: 'AlterUserInfo',
  component: alterUserInfo,
  meta: { requiresAuth: true }
}, {
  path: '/superMember',
  name: 'SuperMember',
  component: superMember,
  meta: { requiresAuth: true }
}, {
  path: '/apiTest',
  name: 'ApiTest',
  component: ApiTest
}, {
  path: '/wallet',
  name: 'Wallet',
  component: Wallet,
  meta: { requiresAuth: true }
}, {
  path: '/wallet/recharge',
  name: 'WalletRecharge',
  component: WalletRecharge,
  meta: { requiresAuth: true }
}, {
  path: '/wallet/withdraw',
  name: 'WalletWithdraw',
  component: WalletWithdraw,
  meta: { requiresAuth: true }
}, {
  path: '/wallet/transactions',
  name: 'WalletTransactions',
  component: WalletTransactions,
  meta: { requiresAuth: true }
}, {
  path: '/wallet/transfer',
  name: 'WalletTransfer',
  component: WalletTransfer,
  meta: { requiresAuth: true }
}, {
  path: '/wallet/rules',
  name: 'WalletRules',
  component: WalletRules,
  meta: { requiresAuth: true }
}, {
  path: '/wallet/overdraft',
  name: 'WalletOverdraft',
  component: WalletOverdraft,
  meta: { requiresAuth: true }
}, {
  path: '/businessLogin',
  name: 'BusinessLogin',
  component: BusinessLogin
}, {
  path: '/businessRegister',
  name: 'BusinessRegister',
  component: BusinessRegister
}, {
  path: '/businessManage',
  name: 'BusinessManage',
  component: BusinessManage,
  meta: { requiresBusinessAuth: true }
}, {
  path: '/foodManage',
  name: 'FoodManage',
  component: FoodManage,
  meta: { requiresBusinessAuth: true }
}, {
  path: '/businessOrders',
  name: 'BusinessOrders',
  component: BusinessOrders,
  meta: { requiresBusinessAuth: true }
}, {
  path: '/businessOrderDetail',
  name: 'BusinessOrderDetail',
  component: BusinessOrderDetail,
  meta: { requiresBusinessAuth: true }
}, {
  path: '/businessStoreSelector',
  name: 'BusinessStoreSelector',
  component: BusinessStoreSelector,
  meta: { requiresAuth: true }  // 只要用户登录即可访问，不需要商家权限
}, {
  path: '/settings',
  name: 'Settings',
  component: Settings,
  meta: { requiresAuth: true }  // 需要用户登录
}, {
  path: '/discovery',
  name: 'Discovery',
  component: Discovery,
  meta: { requiresAuth: true }
}, {
  path: '/businessRefunds',
  name: 'BusinessRefunds',
  component: BusinessRefunds,
  meta: { requiresBusinessAuth: true }
}, {
  path: '/business/wallet',
  name: 'BusinessWallet',
  component: BusinessWallet,
  meta: { requiresBusinessAuth: true }
}]


const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  console.log('路由守卫：', to.path);
  
  // 检查是否存在JWT token
  const hasToken = !!localStorage.getItem('jwt-token');
  
  // 检查路由是否需要用户认证
  if (to.meta.requiresAuth) {
    if (!hasToken) {
      console.log('路由守卫：用户未登录，重定向到 /login');
      next('/login');
    } else {
      console.log('路由守卫：用户已登录，允许访问');
      next();
    }
  } 
  // 检查路由是否需要商家认证
  else if (to.meta.requiresBusinessAuth) {
    if (!hasToken) {
      console.log('路由守卫：商家未登录，重定向到 /businessLogin');
      next('/businessLogin');
      return;
    }
    
    // 检查是否有商家信息
    const businessInfo = localStorage.getItem('business');
    if (!businessInfo) {
      console.log('路由守卫：无商家信息，重定向到 /businessLogin');
      next('/businessLogin');
    } else {
      console.log('路由守卫：商家已登录，允许访问');
      next();
    }
  } 
  else {
    // 不需要认证的路由
    next();
  }
});

export default router
